package com.sina.common.util;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;


import com.sina.common.BuildConfig;

import java.io.File;

import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * 图片压缩工具类（使用Luban）
 * <p>
 * Created by zjh on 2017/7/19.
 */
public class CompressUtil {

    public static synchronized void compress(Context context, String inPath, String outPath, final OnResultListener listener) {
        if (TextUtils.isEmpty(inPath) || TextUtils.isEmpty(outPath)) {
            if (BuildConfig.DEBUG) {
                Toast.makeText(context, "error:file path is empty", Toast.LENGTH_LONG).show();
            }

            if (listener != null) {
                listener.onError("file path is empty");
            }
            return;
        }

        File inFile = new File(inPath);
        File outFile = new File(outPath);
        if (inFile == null || !inFile.exists()) {
            if (BuildConfig.DEBUG) {
                Toast.makeText(context, "error:input file path error", Toast.LENGTH_LONG).show();
            }

            if (listener != null) {
                listener.onError("input path error");
            }

            return;
        }

        compress(context, inFile, outFile, listener);
    }

    public static synchronized void compress(Context context, Uri inUri, Uri outUri, final OnResultListener listener) {
        File outFile = new File(CompressImageUtil.getRealPathFromUri(context, outUri));
        compress(context, inUri, outFile, listener);
    }

    public static synchronized void compress(Context context, Uri inUri, File outFile, final OnResultListener listener) {
        File inFile = new File(CompressImageUtil.getRealPathFromUri(context, inUri));
        if (inFile == null || !inFile.exists()) {
            if (BuildConfig.DEBUG) {
                Toast.makeText(context, "error:input uri error", Toast.LENGTH_LONG).show();
            }

            if (listener != null) {
                listener.onError("input uri error");
            }

            return;
        }

        compress(context, inFile, outFile, listener);
    }

    public static synchronized void compress(final Context context, File inFile, final File outFile, final OnResultListener listener) {
        Luban.with(context).load(inFile).setCompressListener(new OnCompressListener() {
            @Override
            public void onStart() {
                if (listener == null) {
                    return;
                }
                listener.onStart();
            }
            @Override
            public void onSuccess(File file) {
                try {
                    if (outFile == null) {
                        if (BuildConfig.DEBUG) {
                            Toast.makeText(context, "error:output file is null", Toast.LENGTH_LONG).show();
                        }
                        if (listener != null) {
                            listener.onError("output file is null");
                        }
                        return;
                    }
                    if (!outFile.exists()) {
                        outFile.createNewFile();
                    }
                    //对于已经调用了mkdirs的情况做处理
                    if (outFile.isDirectory()) {
                        if (!outFile.getAbsolutePath().endsWith(".jpg") && !outFile.getAbsolutePath().endsWith(".png")) {
                            if (BuildConfig.DEBUG) {
                                Toast.makeText(context, "error:output file disabled (is a directory or not img)", Toast.LENGTH_LONG).show();
                            }
                            listener.onError("output file is a directory");
                            return;
                        }
                        outFile.delete();
                        outFile.createNewFile();
                    }
                    file.renameTo(outFile);
                } catch (Exception e) {
                    if (BuildConfig.DEBUG) {
                        Toast.makeText(context, "error:" + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                    if (listener != null) {
                        listener.onError(e.getMessage());
                    }
                    return;
                }
                if (BuildConfig.DEBUG) {
                    Toast.makeText(context, "compress success", Toast.LENGTH_LONG).show();
                }
                if (listener != null) {
                    listener.onSuccess(outFile);
                }
            }
            @Override
            public void onError(Throwable e) {
                if (BuildConfig.DEBUG) {
                    Toast.makeText(context, "error:" + e.getMessage(), Toast.LENGTH_LONG).show();
                }
                if (listener != null) {
                    listener.onError(e.getMessage());
                }
            }
        }).launch();
    }

    public interface OnResultListener {
        void onStart();

        void onSuccess(File outFile);

        void onError(String error);
    }
}
